<template>
  <div class="recommendation-carousel">
    <h3 class="carousel-title">新专辑推荐</h3>
    <el-carousel
      :interval="5000"
      arrow="hover"
      indicator-position="outside"
      class="album-carousel"
    >
      <el-carousel-item v-for="album in albumList" :key="album.albumId">
        <div class="album-item">
          <el-image
            :src="album.coverUrl"
            :alt="album.albumName"
            class="album-cover"
            fit="cover"
            lazy
          >
            <template #error>
              <div class="image-error">
                <el-icon><PictureRounded /></el-icon>
              </div>
            </template>
          </el-image>
          <div class="album-name">{{ album.albumName }}</div>
        </div>
      </el-carousel-item>
    </el-carousel>
  </div>
</template>

<script setup lang="ts">
import { PictureRounded } from "@element-plus/icons-vue";
import { onMounted, ref } from "vue";
import api from "../services/api";

// 定义专辑数据类型接口
interface Album {
  albumId: number;
  albumName: string;
  artistId: number;
  coverUrl: string;
  releaseDate: string;
  genre: string;
  language: string;
  recordCompany: string;
  description: string;
  trackCount: number;
  status: number;
  createTime: string;
  updateTime: string;
}

// 定义接口返回数据类型
interface AlbumResponse {
  code: number;
  message: string;
  data: Album[];
  timestamp: string;
  requestId: string;
  success: boolean;
}

// 专辑列表数据
const albumList = ref<Album[]>([]);

// 获取专辑数据
const fetchAlbums = async (limit = 6) => {
  try {
    const response = await api.getNewAlbums({ limit });
    console.log("获取新专辑数据:", response);
    if (response.success && response.code === 200) {
      albumList.value = response.data;
    }
  } catch (error) {
    console.error("获取专辑数据失败:", error);
  }
};

// 组件挂载时获取数据
onMounted(() => {
  fetchAlbums(6); // 默认获取6张专辑
});
</script>

<style scoped>
.recommendation-carousel {
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
  padding: 10px 0 0 0;
}

.carousel-title {
  color: #333;
  font-size: 20px;
  margin-bottom: 10px;
  padding-left: 10px;
  border-left: 4px solid #409eff;
}

.album-carousel {
  width: 100%;
  height: 340px;
}

.album-item {
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
}

.album-cover {
  width: 100%;
  height: 100%;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  margin-bottom: 12px;
  transition: transform 0.3s ease;
  object-fit: cover;
}

.album-item:hover .album-cover {
  transform: scale(1.04);
}

/* 专辑名称样式 */
.album-name {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #409eff;
  text-align: center;
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 图片加载失败样式 */
.image-error {
  width: 100%;
  height: 100%;
  background-color: #f5f5f5;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 8px;
}

.image-error .el-icon {
  font-size: 40px;
  color: #909399;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .album-carousel {
    height: 220px;
  }
  .album-cover,
  .album-info-static,
  .image-error {
    width: 100%;
    height: 100%;
    min-width: 0;
    min-height: 0;
  }
  .album-name {
    font-size: 14px;
  }
}
</style>
